package com.ass.wechat.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Repository;

import com.ass.common.generated.dao.WcGameMapper;
import com.ass.common.generated.dao.WcGameRoundMapper;
import com.ass.common.generated.model.WcGame;
import com.ass.common.generated.model.WcGameExample;
import com.ass.common.generated.model.WcGameRound;
import com.ass.common.generated.model.WcGameRoundExample;
import com.ass.common.service.CommonService;

@Repository
public class GuessNumberDaoImpl implements GuessNumberDao {

	@Resource
	private WcGameMapper wcGameMapper;
	
	@Resource
	private WcGameRoundMapper wcGameRoundMapper;

	@Resource
	private CommonService commonService;
	
	/**
	 * 根据游戏id获取游戏的全部回合<br>
	 * 
	 * @param request 请求对象
	 * @param gameId 游戏id
	 * @return
	 */
	@Override
	public List<WcGameRound> findAllRoundByGameId(HttpServletRequest request, int gameId) {
		WcGameRoundExample grExample = new WcGameRoundExample();
		grExample.createCriteria().andGameIdEqualTo(gameId);
		grExample.setOrderByClause(" id asc");
		return wcGameRoundMapper.selectByExample(grExample);
	}

	/**
	 * 获取用户最近一次创建的游戏 <br>
	 * 
	 * @param request 请求对象
	 * @param openId 用户的OpendID
	 * @return
	 */
	@Override
	public WcGame getLastGame(HttpServletRequest request, String openId) {
		WcGameExample gExample = new WcGameExample();
		gExample.createCriteria().andOpenIdEqualTo(openId);
		gExample.setOrderByClause(" id desc");
		List<WcGame> lst = wcGameMapper.selectByExample(gExample);
		if(lst.size() == 0){
			return null;
		}else{
			return lst.get(0);
		}
	}

	/**
	 * 获取用户的战绩
	 * 
	 * @param request 请求对象
	 * @param openId 用户的OpenID
	 * @return HashMap<Integer, Integer>
	 */
	@Override
	public Map<String, String> getScoreByOpenId(HttpServletRequest request, String openId) {
		Map<String, String> m = new HashMap<String, String>();
		m.put("openid", openId);
		List<HashMap<String, String>> lst = commonService.selectList("guessGameSql.getScoreByOpenId", m);
 		Map<String, String> scoreMap = new HashMap<String, String>();
		for(HashMap<String, String> hm : lst){
			scoreMap.put(hm.get("gamestatus"), hm.get("count2").toString());
		}
		return scoreMap;
	}

	/**
	 * 保存游戏信息
	 * 
	 * @param request 请求对象
	 * @param game 游戏对象
	 * @return gameId
	 */
	@Override
	public int saveGame(HttpServletRequest request, WcGame game) {
		wcGameMapper.insertSelective(game);
		return game.getId();
	}

	
	/**
	 * 保存游戏的回合信息
	 * 
	 * @param request 请求对象
	 * @param gameRound 游戏回合对象
	 */
	@Override
	public void saveGameRound(HttpServletRequest request, WcGameRound gameRound) {
		wcGameRoundMapper.insertSelective(gameRound);
		
	}

	
	/**
	 * 根据游戏id修改游戏状态和完成时间
	 * 
	 * @param request 请求对象
	 * @param gameId 游戏id
	 * @param gameStatus 游戏状态（0:游戏中 1:成功 2:失败 3:取消）
	 * @param finishTime 游戏完成时间
	 */
	@Override
	public void updateGame(HttpServletRequest request, int gameId, String gameStatus, String finishTime) {
		WcGame g = new WcGame();
		g.setId(gameId);
		g.setGameStatus(gameStatus);
		g.setFinishTime(finishTime);
		wcGameMapper.updateByPrimaryKeySelective(g);
		
	}

	@Override
	public String getCurrentAnswer(String openId) {
		WcGameExample gExample = new WcGameExample();
		gExample.createCriteria().andOpenIdEqualTo(openId);
		gExample.setOrderByClause(" id desc");
		List<WcGame> lst = wcGameMapper.selectByExample(gExample);
		if(lst.size() > 0){
			return lst.get(0).getGameAnswer();
		}else{
			return null;
		}
	}

}
